var newsTable;
//获取试题目信息
function getExamQuestionsList(name) {
    if (typeof newsTable !== 'undefined') {
        newsTable.fnPageChange('first');
        return;
    }
    
    var i = 1;
	var lid=$("#liveId").val();
    newsTable = $("#newsList").DataTable({
        "sAjaxSource": _path + "/examQuestions/queryByPage",
        "fnServerParams": function (aoData) {
            aoData.push(
                { "name": "questionName", "value": $("#questionName").val() },
                { "name": "fromDate", "value": $("#fromDate").val() },
                { "name": "toDate", "value": $("#toDate").val() },
                { "name": "projectId", "value": $("#projectId").val() }
            );
        },
        "sServerMethod": "POST",
        "sAjaxDataProp": "dataList",
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            },
            { "sTitle": "题目名字", "sWidth": "20%" ,"mData": 
	            	function(data){
	        		var temps = '<a href="' + _path + '/examQuestions/edit?id=' + data.id + '&projectId=' + data.projectId + '&liveId=' + lid + '">'+data.questionName+'</a>';
	        		return temps;
	        	}
	        },
	        { "sTitle": "显示顺序", "sWidth": "20%" ,"mData": 
	            	function(data){
	        		var temps ="<input type=\"text\" id=\"sort"+data.id+"\"  style=\"width:30px;\" value=\""+data.sort+"\" onblur=\"updateSort('"+data.id+"','"+name+"');\">"
	        		return temps;
        		}
	        },
	        
            { "sTitle": "创建时间", "mData": "insertTime","sWidth": "10%" },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                
                		var status = '<a href="' + _path + '/examQuestions/edit?id=' + data.id + '&projectId=' + data.projectId + '&liveId=' + lid + '" class="btn default btn-xs purple"><i class="fa fa-edit" title="编辑"></i></a>';
                        status += '&nbsp;<a href="' + _path + '/examOptions/list?questionId=' + data.id + '&projectId=' + data.projectId + '&liveId=' + lid + '"  class="btn default btn-xs green">选择项</a>';
                        status += '&nbsp;<a href="javascript:inquiry('+data.id+')"  class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';

                        return status;
                }
            }
        ]
    });
}


function inquiry(id){
	layer.confirm('您确定要删除吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			layer.closeAll('dialog');
			$.ajax({
				url :  _path + "/examQuestions/del/" + id,
				type : "get",
				dataType : "json",
				async : false,
				error : function(request) {
					alert("连接错误！");
				},
				success : function(data) {
					  if(data.msg=='成功'){
						  layer.close
						  App.bubble(200, "删除成功");
						  getExamQuestionsList();
					  }else{
						  App.bubble(200, "删除异常");
					  }
				}
			});
		}, function(){
		
		});
}

//修改显示顺序
function updateSort(id,name){
	var sort = document.getElementById("sort"+id).value;
	$.ajax({										
	    url: _path + "/examQuestions/updateSort/?id="+id+"&sort="+sort,
	    type: "POST",
	    dataType: "json",
	    async: false,
	    success: function(jsonMap) {
	    	var result = jsonMap.result;
	    	getExamQuestionsList(name);
	    }
	});
}

//获取所有项目名称
function queryAllProject(pId){
	 $.ajax({										
		    url: _path + "/examQuestions/queryAllProject",
		    type: "POST",
		    dataType: "json",
		    async: false,
		    success: function(jsonMap) {
		    	var list = jsonMap.dataList;
		    	$("#projectId").html("");
		    	$("#projectId").append("<option selected value=''>请选择</option>");
		        if(list.length>0){
		        	for(var i=0;i<list.length;i++){
						var projects = list[i];
						var op ="";
						  if ( pId == projects.id) {
							  op = "<option value="+projects.id+" selected>"+projects.name+"</option>";
						  }else{
							  op = "<option value="+projects.id+" >"+projects.name+"</option>";
						  }
						$("#projectId").append(op);
		        	}
		        }
		    }
		});
}


//获取所有项目名称
function queryAllProjectjp(pId){
	 $.ajax({										
		    url: _path + "/examQuestions/queryAllProject",
		    type: "POST",
		    dataType: "json",
		    async: false,
		    success: function(jsonMap) {
		    	var list = jsonMap.dataList;
		    	$("#projectId").html("");
		    	$("#projectId").append("<option selected value=''>请选择</option>");
		        if(list.length>0){
		        	for(var i=0;i<list.length;i++){
						var projects = list[i];
						var op ="";
						  if ( pId == projects.id) {
							  op = "<option value="+projects.id+" selected>"+projects.name+"</option>";
						  }
						$("#projectId").append(op);
		        	}
		        }
		    }
		});
}

//获取所有测评标签
function queryAllLabel(labelName){
	 $.ajax({										
		    url: _path + "/examQuestions/queryAllLabel",
		    type: "POST",
		    dataType: "json",
		    async: false,
		    success: function(jsonMap) {
		    	var list = jsonMap.dataList;
		    	$("#labelInfo").html("");
		        if(list.length>0){
		        	if(null == labelName){  //添加时获取所有标签
		        		for(var i=0;i<list.length;i++){
							var labels = list[i];
							var label = "<label><input type='checkbox' name='ck' value="+labels.labelName+">"+labels.labelName+"</label>&nbsp;&nbsp;";
							$("#labelInfo").append(label);
			        	}
		        	}else{  //修改时 比对判断标签是否被选中
		        		for(var i=0;i<list.length;i++){
							var labels = list[i]; 
							var names = labelName.split("|"); 
							var label = "<label><input type='checkbox' name='ck' value="+labels.labelName+">"+labels.labelName+"</label>&nbsp;&nbsp;";
							for(var j=0;j<names.length;j++){
								var localLabel = names[j];
								if(labels.labelName == localLabel){
									label="<label><input type='checkbox' name='ck' checked value="+labels.labelName+">"+labels.labelName+"</label>&nbsp;&nbsp;";
								}
							}
							$("#labelInfo").append(label);
			        	}
		        	}
		        	
		        }
		    }
		});
}

